package servlet;

import model.UserInfo;
import service.UseInfoService;
import utils.MyBatisUtil;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

import static java.lang.Thread.sleep;

@WebServlet(name = "UpdateUseInfoServlet", value = "/UpdateUseInfoServlet")
public class UpdateUseInfoServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        UseInfoService uis = new UseInfoService();
        UserInfo ui = new UserInfo();
        if (request.getParameter("id") != null) {
            ui.setUserId(Integer.valueOf(request.getParameter("id")));
            if (request.getParameter("username") != null)
                ui.setUserName(request.getParameter("username"));
            if (request.getParameter("usersex") != null)
                ui.setUserSex(request.getParameter("usersex"));
            if (request.getParameter("userpass") != null)
                ui.setUserPass(request.getParameter("userpass"));
            if (request.getParameter("userage") != null)
                ui.setUserAge(Integer.valueOf(request.getParameter("userage")));
            int i = uis.updateUser(ui);
            if (i == 1) response.getWriter().println("success");
            else response.getWriter().println("failed");
            MyBatisUtil.closeSession();
            request.getRequestDispatcher("/SelectAllServlet").forward(request, response);
        } else {
            response.getWriter().println("failed");
        }
    }
}
